const glob = require('glob')
const { spawnSync } = require('child_process')
const env = process.argv[2] || 'test'

const modules = glob.sync('./src/modules/*/main.js')
let cmd = ''
modules.forEach((modulePath, i) => {
    let modulePathArr = modulePath.split('/')
    let module = modulePathArr[modulePathArr.length - 2]
    if (env === 'prod') {
        // 生产环境
        cmd += 'npm run build:prod ' + module
    } else if (env === 'test') {
        // 测试环境
        cmd += 'npm run build:test ' + module
    }
    if (i !== (modules.length - 1)) {
        cmd += ' & '
    }
})

cmd = cmd.substr(0, cmd.length)
spawnSync(cmd, [], {
    shell: true,
    detached: true
})

console.log('===== ' + env + ' 打包完成 =====')
